using System;
using System.Collections.Generic;
using System.IO;

namespace WorldRacing2.Scenario.WR2TerrainLoader
{
    public class IDXFile : IDisposable
    {
        #region Private Fields

        private List<ushort> indices;

        #endregion Private Fields

        #region Internal Constructors

        internal IDXFile()
        {
            this.indices = new List<ushort>();
        }

        #endregion Internal Constructors

        #region Private Destructors

        ~IDXFile()
        {
            Dispose();
            if (this.indices != null)
                this.indices.Clear();
            this.indices = null;
        }

        #endregion Private Destructors

        #region Public Properties

        //int numberOfIndices;
        public List<ushort> AllIndices
        {
            get { return indices; }
            set { indices = value; }
        }

        #endregion Public Properties

        #region Public Methods

        public void Dispose()
        {
            if (this.indices != null)
                this.indices.Clear();
            this.indices = null;
        }

        #endregion Public Methods

        #region Internal Methods

        internal static IDXFile FromFile(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            IDXFile file = IDXFile.FromStream(br);

            if (!File.Exists(filename))
            {
                throw new FileNotFoundException(filename);
            }

            br.Close();
            fs.Close();

            return file;
        }

        internal static IDXFile FromStream(BinaryReader br)
        {
            IDXFile idxFile = new IDXFile();
            int numberOfIndices = br.ReadInt32();

            idxFile.indices = new List<ushort>();
            for (int i = 0; i < numberOfIndices; i++)
            {
                idxFile.indices.Add(br.ReadUInt16());
            }

            return idxFile;
        }

        internal void Reset()
        {
            this.indices.Clear();
        }

        internal void Save(string scenarioFolder)
        {
            string[] temp = scenarioFolder.Split('\\');
            string snenarioName = temp[temp.Length - 1];
            string directory = scenarioFolder + "\\V1\\";
            string filePath = scenarioFolder + "\\V1\\" + snenarioName + ".idx";

            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            FileStream fs = new FileStream(filePath, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(this.indices.Count);

            for (int i = 0; i < this.indices.Count; i++)
            {
                bw.Write(this.indices[i]);
            }

            bw.Close();
            fs.Close();
        }

        #endregion Internal Methods

        #region Internal Structs

        internal struct Block
        {
            #region Private Fields

            private List<ushort> indices;
            private int offset;

            #endregion Private Fields

            #region Public Constructors

            public Block(int offset, List<ushort> indices)
            {
                this.offset = offset;
                this.indices = new List<ushort>(indices);
            }

            #endregion Public Constructors

            #region Public Properties

            public List<ushort> Indices
            {
                get { return indices; }
                set { indices = value; }
            }

            #endregion Public Properties
        }

        #endregion Internal Structs
    }
}